<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2017/4/3
 * Time: 上午9:09
 * 会员管理
 */
class Users extends MY_Controller
{
  function __construct(){
	parent::__construct();
    $this->load->model('User_model');
    $this->smarty->assign("menu", "users");
  	$parent_id = 7;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	  $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => " 会员管理", "curr" => '/users', "list" => $menu_list);
  	$this->smarty->assign("left_menu_min", $this->left_menu_min);

		// $this->smarty->assign("menu", 'classes');

	}



	public $offset = 20;
	public function index($nick = 0, $mobile = 0, $limit=0)
	{
		$post_data = $this->input->post();
		if($post_data){
			$nick = !empty($post_data['nick']) ? $post_data['nick'] : 0;
			$mobile = !empty($post_data['mobile']) ? $post_data['mobile'] : 0;
		}
		//获取会员list
		$conditions['nick'] = $nick;
		$conditions['mobile'] = $mobile;
		$conditions['school_id'] = $this->user_data['school_id'];
		$users = $this->User_model->get_users_list($limit,$this->offset,$conditions);
		$this->smarty->assign("users", $users['result']);

		$this->load->library('pagination');
		$page_config['base_url'] = '/users/index/'.$nick.'/'.$mobile;
		$page_config['total_rows'] = $users['total_count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 5;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("conditions", $conditions);


		$this->smarty->display("user/user_list.html");

	}

	function setvip($user_id){
		$post_data = $this->input->post();
		if($post_data){
			$this->User_model->set_user_vip($user_id,$post_data['end_day']);
			header("Location:/users");exit;
		}

		$user = $this->User_model->get_user_by_id($user_id);
		$this->smarty->assign("user", $user);


		$this->smarty->assign("menu", "users");
		$this->smarty->display("user/set_vip.html");
	}

	function order_list($uid = 0, $status='all', $order_type = 0, $order_num=0, $st = 0, $et = 0, $limit = 0){
		$user_info = get_field_by_where("user", "id, school_id", "id='{$uid}'");
		if(!$user_info || ($user_info['school_id'] != $this->user_data['school_id'])){
			show_error_message("/users", "用户不存在");
		}else{
			if($this->input->post()){
				$post_data = $this->input->post();
				$status = isset($post_data['status']) ? $post_data['status'] : 'all';
				$order_type = !empty($post_data['order_type']) ? $post_data['order_type'] : 0;
				$order_num = !empty($post_data['order_num']) ? urldecode($post_data['order_num']) : 0;
				$st = !empty($post_data['st']) ? $post_data['st'] : 0;
				$et = !empty($post_data['et']) ? $post_data['et'] : 0;
			}

			$data = array( 'status' => $status, 'order_type' => $order_type, 'order_num' => $order_num, 'st' => $st,'et' => $et,'uid' => $uid);
			$this->load->model('Order_model');
			$result = $this->Order_model->get_order_list_by_school($limit, $this->offset, $this->user_data['school_id'], $data);
			$this->smarty->assign("order_list", $result['result']);

			$this->load->library('pagination');
			$page_config['base_url'] = '/users/order_list/'.$data['uid'].'/'.$data['status'].'/'.$data['order_type'].'/'.$data['order_num'].'/'.$data['st'].'/'.$data['et'];
			$page_config['total_rows'] = $result['total_count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 9;
			$this->smarty->assign("data", $data);
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("user/order_list.html");
		}
	}

	function detail($uid, $data_type = 0, $lost_status = 0, $limit = 0){
		$conditions['uid'] = $uid;
		$conditions['data_type'] = $data_type;
		$conditions['lost_status'] = $lost_status;
		if($_POST){
			$post_data = $this->input->post();
			if(!empty($post_data['data_type'])) $conditions['data_type'] = $post_data['data_type'];
			
			if(!empty($post_data['lost_status'])) $conditions['lost_status'] = $post_data['lost_status'];
		}
		
		$data_list = $this->User_model->get_user_item_terms($conditions, $limit, $this->offset);
		if(!empty($data_list['count'])){
			$shixun_data = json_decode(file_get_contents("http://123.206.137.187:804/HandlerData.ashx?action=GetZhangtaoInfo"),true);
			foreach ($data_list['result'] as $key => $value) {
				if($value['data_type'] == 5 && !empty($shixun_data['rows']) && count($shixun_data['rows']) > 0){
					$data_list['result'][$key]['name'] = '实训数据接口方已删除';
					foreach ($shixun_data['rows'] as $k => $v) {
						if($v['Id'] == $value['data_id']){
							$data_list['result'][$key]['name'] = $v['ztName'];
						}
					}
					$data_list['result'][$key]['type_name'] = '实训';
				}elseif($value['data_type'] == 1 || $value['data_type'] == 2 || $value['data_type'] == 3){
					if($value['type'] == 1){
						$data_list['result'][$key]['type_name'] = '视频课';
					}elseif($value['type'] == 2){
						$data_list['result'][$key]['type_name'] = '直播课';	
					}else{
						$data_list['result'][$key]['type_name'] = '面授课';	
					}
					$data_list['result'][$key]['name'] = $value['title'];
				}elseif($value['data_type'] == 4){
					$data_list['result'][$key]['type_name'] = '题库';
					$data_list['result'][$key]['name'] = $value['s_title'];
				}
			}
			// print_r($data_list);
		}
		$this->load->library('pagination');
		$page_config['base_url'] = '/users/detail/'.$uid.'/'.$data_type.'/'.$lost_status;
		$page_config['total_rows'] = $data_list['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 6;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("data_list", $data_list);
		$this->smarty->assign("time", time());
		$this->smarty->display("user/detail.html");
	}

	function delete_student_item($id){
		$item_info = get_field_by_where("o2o_student_item_terms", "*", "id='{$id}'");
		if(!$item_info){
			show_message($_SERVER['HTTP_REFERER'], "数据不存在");
			exit;
		}
		if($item_info['school_id'] != $this->user_data['school_id']){
			show_message($_SERVER['HTTP_REFERER'], "数据不存在");
			exit;
		}
		$this->db->where("id",$id);
		$this->db->delete("o2o_student_item_terms");
		show_message($_SERVER['HTTP_REFERER'], "删除成功");
	}

	function student_item($user_id, $data_type = 1, $data_id = 0){
		if($_POST){
			$post_data = $this->input->post();
			if(!empty($post_data['data_type'])){
				$data_type = $post_data['data_type'];
			}
			if(!empty($post_data['data_id'])){
				$data_id = $post_data['data_id'];
			}
		}
		$user_info = get_field_by_where("user", "*", "id='{$user_id}'");
		if(!$user_info){
			show_message($_SERVER['HTTP_REFERER'], "会员不存在");
			exit;
		}
		if($user_info['school_id'] != $this->user_data['school_id']){
			show_message($_SERVER['HTTP_REFERER'], "会员不存在");
			exit;
		}
		if($data_type == 1 && $data_type < 4){
			if($data_type == 1){
				$type_name = '视频课';
			}elseif($data_type == 2){
				$type_name = '直播课';
			}else{
				$type_name = '面授课';
			}
			$data = $this->User_model->get_school_course($data_id);
			if(!empty($data_id) && !$data){
				show_message($_SERVER['HTTP_REFERER'], "该课程不存在或者已经下架");
				exit;
			}
		}elseif($data_type == 4){
			$type_name = '题库';
			$data = $this->User_model->get_school_tiku($data_id);
			if(!empty($data_id) && !$data){
				show_message($_SERVER['HTTP_REFERER'], "该题库不存在或者已经下架");
				exit;
			}
		}else{
			$type_name = '实训';
			$data = json_decode(file_get_contents("http://123.206.137.187:804/HandlerData.ashx?action=GetZhangtaoInfo"),true);
			if(isset($data['rows']) && count($data['rows']) > 0){
				$rows = $data['rows'];
				if(!empty($data_id)){
					$data = array();
					foreach ($rows as $key => $value) {
						if($value['Id'] == $data_id){
							$data[] = $value;
						}
					}
					if(!$data){
						show_message($_SERVER['HTTP_REFERER'], "该实训数据不存在，可能已被接口方删除");
						exit;
					}
				}else{
					$data = $rows;
				}
				
				
				
			}
		}
		if(!$data){
			show_message($_SERVER['HTTP_REFERER'], "没有数据");
			exit;
		}
		if($_POST){
			$time = time();
			if(!empty($post_data['data_id']) && !empty($post_data['data_type'])){
				$data_info = $data[0];

				//如果是系统题库、网课、实训就要扣除激活码（直播客和面授课除外）
				if((isset($data_info['school_id']) && $data_info['school_id'] == 0) || $data_type == 5){
					if($post_data['data_type'] == 1 || $post_data['data_type'] == 4 || $post_data['data_type'] == 5){
						if(empty($post_data['code'])){
							show_message($_SERVER['HTTP_REFERER'], "请选择激活码");exit;
						}
						$this->load->model("Order_course_auths");
						$code_info = $this->Order_course_auths->get_order_record_codes($data_id, $data_type, 0, $post_data['code']);
						if(!$code_info){
							show_message($_SERVER['HTTP_REFERER'], "激活码不存在");exit;
						}
						$code_info = $code_info[0];
						if($code_info['status'] != 1){
							show_message($_SERVER['HTTP_REFERER'], "该激活码已经被使用");exit;
						}
					}
					// print_r($code_info);exit;
					if($post_data['data_type'] == 1){
						$code_table_name = 'o2o_school_system_video_course_codes';
						/*$time = time();
						$use_code_data['course_id'] = $post_data['data_id'];
						$use_code_data['course_type'] = $post_data['data_type'];
						$use_code_data['user_id'] = $user_id;
						$use_code_data['code'] = 'schoo_course';
						$use_code_data['order_num'] = 'school_add';
						$use_code_data['school_id'] = $this->user_data['school_id'];
						$use_code_data['add_time'] = $time;
						$use_code_data['effective_time'] = $code_info['expire_day'];
						$this->db->insert("o2o_student_course", $use_code_data);*/
					}elseif($post_data['data_type'] == 4){
						$code_table_name = 'o2o_school_system_topic_codes';
					}else{
						$code_table_name = 'o2o_school_system_shixun_codes';
					}
					//修改激活码使用状态
					if(!empty($code_table_name)){
						$update_data = array("status" => 2, "use_uid" => $user_id, "use_time" => $time);
						$this->db->update($code_table_name, $update_data, array("id" => $code_info['id']));
					}
					$effective_time = $code_info['expire_day'];
				}else{
					if($post_data['data_type'] == 4){
						$effective_time = $data_info['expire_day'];
					}elseif($post_data['data_type'] == 5){
						$shixun_info = get_field_by_where("o2o_school_sx_packages", "*", "school_id='{$this->user_data['school_id']}' AND sx_id='{$data_info['Id']}' AND status=1");
						if(!$shixun_info){
							show_message($_SERVER['HTTP_REFERER'], "该实训未上架");exit;
						}
						echo $effective_time = $shixun_info['expire_day'];exit;
					}else{
						$effective_time = $data_info['effective_time'];
					}
				}
				$where = "data_type='{$post_data['data_type']}' AND data_id='{$post_data['data_id']}' AND uid='{$user_id}'";
				$terms_info = get_field_by_where("o2o_student_item_terms", "*", $where);
				$terms_data['school_id'] = $this->user_data['school_id'];
				$terms_data['data_type'] = $post_data['data_type'];
				$terms_data['data_id'] = $post_data['data_id'];
				$terms_data['uid'] = $user_id;
				$terms_data['lose_time'] = 3600 * 24 * $effective_time + $time;
				$terms_data['last_order_id'] = '';
				if($terms_info){
					if($terms_info['lose_time'] > $time){
						$terms_data['lose_time'] = 3600 * 24 * $effective_time + $terms_info['lose_time'];
					}
					$this->db->where("id", $terms_info['id']);
					$this->db->update("o2o_student_item_terms", $terms_data);
				}else{
					$this->db->insert("o2o_student_item_terms", $terms_data);
				}
				show_message($_SERVER['HTTP_REFERER'], "操作成功");exit;
			}
		}
		$this->smarty->assign("data", $data);
		$this->smarty->assign("type_name", $type_name);
		$this->smarty->assign("data_type", $data_type);
		$this->smarty->assign("user", $user_info);
		$this->smarty->display("user/student_item.html");
	}

	function school_order_records($data_id = 0, $data_type = 1, $sekected_code = 0){
		$this->load->model("Order_course_auths");
		$result = $this->Order_course_auths->get_order_record_codes($data_id, $data_type);
		$this->smarty->assign("codes", $result);
		$this->smarty->assign("sekected_code", $sekected_code);
		$this->smarty->display("user/school_order_records.html");
	}
}
